
import java.util.Date;
import java.util.UUID;
import java.text.SimpleDateFormat;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Calendar;

// 电影票类
class Ticket {
    private String ticketId;
    private Show show;
    private Seat[] seats;
    private double price;
    private boolean isRefunded;
    private Date saleTime;

    public Ticket(Show show, Seat[] seats, double price) {
        this.ticketId = UUID.randomUUID().toString();
        this.show = show;
        this.seats = seats;
        this.price = price;
        this.isRefunded = false;
        this.saleTime = new Date();
    }

    // 显示票信息
    public void displayInfo() {
        System.out.println("票号: " + ticketId);
        System.out.println("影片: " + show.getMovie().getName());
        System.out.println("放映时间: " + show.getShowTime());
        System.out.println("放映厅: " + show.getHall().getName());
        System.out.print("座位: ");
        for (Seat seat : seats) {
            System.out.print(seat.getSeatNumber() + " ");
        }
        System.out.println();
        System.out.println("价格: " + price);
        System.out.println("状态: " + (isRefunded ? "已退票" : "有效"));
    }

    // Getter和Setter
    public String getTicketId() { return ticketId; }
    public Show getShow() { return show; }
    public Seat[] getSeats() { return seats; }
    public double getPrice() { return price; }
    public boolean isRefunded() { return isRefunded; }
    public void setRefunded(boolean refunded) { this.isRefunded = refunded; }
    public Date getSaleTime() { return saleTime; }
}

// 售票类
class TicketSale {
    public static Ticket sellTicket(Show show, Seat[] seats) {
        // 检查座位是否都可用
        for (Seat seat : seats) {
            if (seat.isSold()) {
                System.out.println("座位 " + seat.getSeatNumber() + " 已售出");
                return null;
            }
        }

        // 计算总票价
        double totalPrice = calculateTotalPrice(show, seats);

        // 更新座位状态
        for (Seat seat : seats) {
            seat.setSold(true);
        }

        // 创建票
        Ticket ticket = new Ticket(show, seats, totalPrice);
        TicketManager.addTicket(ticket);
        return ticket;
    }

    private static double calculateTotalPrice(Show show, Seat[] seats) {
        double basePrice = show.getPrice();
        // 根据影片评分调整价格
        double ratingFactor = 1.0;
        if (show.getMovie().getRating() >= 9.0) {
            ratingFactor = 1.2;
        } else if (show.getMovie().getRating() >= 8.0) {
            ratingFactor = 1.1;
        }
        return basePrice * seats.length * ratingFactor;
    }
}

// 退票类
class TicketRefund {
    public static boolean refundTicket(String ticketId) {
        Ticket ticket = TicketManager.getTicketById(ticketId);
        if (ticket == null) {
            System.out.println("票号不存在");
            return false;
        }

        if (ticket.isRefunded()) {
            System.out.println("该票已退票");
            return false;
        }

        // 检查退票时间
        long timeDiff = ticket.getShow().getShowTime().getTime() - System.currentTimeMillis();
        if (timeDiff < 0) {
            System.out.println("影片已放映，不能退票");
            return false;
        }

        // 退票规则：提前24小时以上全额退款，否则收取10%手续费
        double refundAmount = timeDiff >= 24 * 60 * 60 * 1000 ? 
                             ticket.getPrice() : ticket.getPrice() * 0.9;

        // 更新座位状态
        for (Seat seat : ticket.getSeats()) {
            seat.setSold(false);
        }

        // 更新票状态
        ticket.setRefunded(true);

        System.out.println("退票成功，退款金额: " + refundAmount);
        return true;
    }
}

// 日志管理类
class LogManager {
    public static void recordLog(String operation, Date time, String operator) {
        try (FileWriter writer = new FileWriter("cinema_log.txt", true)) {
            String logEntry = String.format("[%s] [%s] %s 执行操作: %s%n",
                    new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(time),
                    operator, operator, operation);
            writer.write(logEntry);
        } catch (IOException e) {
            System.err.println("记录日志失败: " + e.getMessage());
        }
    }
}